package ex2.pukepai;

import java.util.*;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/10 13:54
 * @DESCRIBE
 */
public class Poker {
    Card[] cards;
    String[] suits = {"黑桃","红桃","梅花","方片"};

    public Poker(){
        cards = new Card[20];
        int index = 0;
        for (int i = 0; i < suits.length; i++) {
            for (int j = 10 ; j <= 13 ; j++){
                Card card = new Card(suits[i],j);
                cards[index++] = card;
//                System.out.println(card);
            }
        }
    }

    // 输出52张牌
    public void outPutCard(){
        int index = 0;
        for (int i = 0; i < suits.length; i++) {
            for (int j = 10; j <= 13; j++) {
                System.out.print(cards[index++] + " ");
            }
            System.out.println();
        }
    }

    //洗牌
    public void shuffle(){
        Random random = new Random();
        for (int i = 0; i < 20 ; i++) {
            int index = random.nextInt(20);
            Card temp = cards[i];
            cards[i] = cards[index];
            cards[index] = temp;
        }
    }

    public List<Card> getOneHand(){
        List<Card> hand = new ArrayList<>();
        this.shuffle();
        for (int i = 0; i < 5 ; i++){
            hand.add(cards[i]);
        }
        return hand;
    }


    public static void main(String[] args) {
        Poker poker = new Poker();
        poker.outPutCard();
        poker.shuffle();
        System.out.println();
        poker.outPutCard();

        List<Card> hand = poker.getOneHand();
        System.out.println(hand);

        String handType = poker.getHandType(hand);
        System.out.println();
        System.out.println(handType);
    }

    public Card getOneCard(int index){
        return cards[index];
    }

    public String getHandType(List<Card> hand){
        Set<String> suitSet = new HashSet<>();
        Set<Integer> rankSet = new HashSet<>();
        List<Integer> rankList = new ArrayList<>();
        Map<Integer, Integer> CountMap = new HashMap<>();
        for (Card card : hand) {
            suitSet.add(card.suit);
            rankSet.add(card.rank);
            rankList.add(card.rank);
        }
        Collections.sort(rankList);

        boolean bIsStraight = false;
        boolean bIsSameColor = false;
        if (suitSet.size() == 1){
            bIsSameColor = true;
//            return "同花";
        }
        if (rankSet.size() == 5 && rankList.get(4) - rankList.get(0) == 4){
            bIsStraight = true;
//            return "顺子";
        }
        if (bIsSameColor && bIsStraight) {
            return "同花顺";
        } else if (bIsSameColor) {
            return "同花";
        } else if (bIsStraight) {
            return "顺子";
        }

        if (rankSet.size() == 4 ){
            return "一对";
        }
        if (rankSet.size() == 5){
            return "杂牌";
        }
        if (rankSet.size() == 2){
            //4带1or3带2
            if(CountMap.containsValue(4)){
                return "四条";
            }
            else{
                return "满堂红";
            }
        }
        if(rankSet.size() == 3){
            //311or221
            if(CountMap.containsValue(3)){
                return "三条";
            }
            else{
                return "两对";
            }
        }
        return "unknown";
    }
}
